﻿
function wondownInfo(lat, long, mess) {
    map.closeInfoWindow();
    var position = new VLatLng(lat, long);
    map.setCenter(position, 19);
    var strHTML = mess;
    map.openInfoWindow(position, strHTML);
}
/*
Load toàn bộ IDStation vào comboBox và add vào bản đồ một trạm
*/
/// remove tuyến xe buýt hiện tại
function removeRouteExist() {
    var _lengthWay = arrayStationToStation.length;
    for (i = 0; i < _lengthWay; ++i) {
        map.removeOverlay(arrayStationToStation[i]);
    }
    arrayStationToStation = new Array();
}
////

function _loadAllStation(listAllStation) {
    var length = listAllStation.length;

    for (i = 0; i < length; ++i) {
        ///add one bus station
        if (listAllStation[i].Latitude != 0 && listAllStation[i].Longitude != 0) {
            var pt = new VLatLng(listAllStation[i].Latitude, listAllStation[i].Longitude);
            var marker = new VMarker(pt, new VIcon("Images/bus.png", new VSize(16, 16)));
            arrayMarker.push(marker); 
            addOneMaker(listAllStation[i].IDStation, marker);
        }
    }

}


function _getStationInfo(combox, _isUpdate) {
    isUpdae = _isUpdate;
    var IDStration = (combox.options[combox.selectedIndex].value);
    if (IDStration) {
        getStationInfo(IDStration);
    }
}
// set value to textbox and set position center of map at position is bus station
function setStationInfoToTb(station) {

    if (isUpdate == true) {
        document.getElementById("txt_SoNha").value = station.AddressNumber;
        document.getElementById("txt_TenDuong").value = station.StreetName;
        document.getElementById("txt_Quan").value = station.DistrictName;
        document.getElementById("txt_ToaDo").value = station.Latitude + "," + station.Longitude;
    }
    // If latitude and longitude is exist then you set one tooltip at position ò station
    if (station.Latitude != 0 && station.Longitude != 0) {
        wondownInfo(station.Latitude, station.Longitude, "Bạn vừa chọn trạm này!!!");
    }

}

// convert from address to latitude and longitude
function convertAddressToLongLat(address) {
    var longLat;
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            longLat = results[0].geometry.location.toString().replace('(', '').replace(')', '').split(',', 2);
            if (longLat.length > 0) {
                var position = new VLatLng(longLat[0], longLat[1]);
                map.setCenter(position, map.getLevel());
                if (_markerFindAddress != null) {
                    map.removeOverlay(_markerFindAddress);
                }
                _markerFindAddress = new VMarker(position, new VIcon());
                map.addOverlay(_markerFindAddress);
            }
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

function getAddressFromInput() {
    var address = document.getElementById("txt_SoNha").value + " " + document.getElementById("txt_TenDuong").value + ", " + document.getElementById("txt_Quan").value;
    return address;
}

//Convert Address to Longitude and Latitude
function _convertAddressToLongLat() {
 var address=getAddressFromInput() + ", " + "Ho Chi Minh, VietNam";
   convertAddressToLongLat(address);
    // If latitude and longitude is exist then you set one tooltip at position ò station 

}

function _updateStationInfo() {
    var combox = document.getElementById("combo_IDTram");
    var IDStration = (combox.options[combox.selectedIndex].value);
    var addressNumber = document.getElementById("txt_SoNha").value;
    var streetName = document.getElementById("txt_TenDuong").value;
    var district = document.getElementById("txt_Quan").value;
    var latLong = document.getElementById("txt_ToaDo").value.split(',');

    updateStationInfo(IDStration, addressNumber, streetName, district, latLong[0], latLong[1]);
    
    document.getElementById("txt_SoNha").value="";
    document.getElementById("txt_TenDuong").value="";
    document.getElementById("txt_Quan").value = "";
    document.getElementById("txt_ToaDo").value = "";
}

//Add Station
function _insertStation() {
    var addressNumber = document.getElementById("txt_SoNha").value;
    var streetName = document.getElementById("txt_TenDuong").value;
    var district = document.getElementById("txt_Quan").value;
    var latLong = document.getElementById("txt_ToaDo").value.split(',');
    isInsert = true;
    insertStation(addressNumber, streetName, district, latLong[0], latLong[1]);
}
//insertStationToStation(id1,id2)
function _insertStationToStation() {

    var combo_Start = document.getElementById("combo_TramBatDau");
    var IDEnd = document.getElementById("IDTramDen").value;

    var idStration_Start = (combo_Start.options[combo_Start.selectedIndex].value);
 
    var idStration_End = IDEnd;
    insertStationToStation(idStration_Start, idStration_End);

}
//insertStationToStation(id1,id2)
function _updateStationToStation() {
    var combo_Start = document.getElementById("combo_TramBatDau");
    var combo_End = document.getElementById("combo_TramDen");
    var nodes = document.getElementById("txt_nodes").value;
    var idStration_Start = (combo_Start.options[combo_Start.selectedIndex].value);
    var idStration_End = (combo_End.options[combo_End.selectedIndex].value);
    updateStationToStation(idStration_Start, idStration_End,nodes);
}
//get way station from id
function _getWayStationToOtherStation(id) {
    getWayStationToOtherStation(id);
}
function _getStationToStation() {

    var combo_Start = document.getElementById("combo_TramBatDau");
    var idStration_Start = (combo_Start.options[combo_Start.selectedIndex].value);
    getStationToStation(idStration_Start);
   
}
function _getStationToStationDetail() {

    var combo_Start = document.getElementById("combo_TramBatDau");
    var idStration_Start = (combo_Start.options[combo_Start.selectedIndex].value);
    var combo_End = document.getElementById("combo_TramDen");
    var idStration_End = (combo_End.options[combo_End.selectedIndex].value);     
    getStationToStationDetail(idStration_Start, idStration_End);
}

//get all bus route
function _getBusRoutes() {

    getBusRoutes();
}

//insert route
function _insertBusRoute() {
    insertBusRoute();
}
//get one route
function _getBusRoute(combo) {
    var id = combo.options[combo.selectedIndex].value;
    getBusRoute(id);
}
//update one route
function _updateBusRoute() {
    var combo = document.getElementById("combo_Tuyen");
    var idBusRoute = combo.options[combo.selectedIndex].value;
    var number = document.getElementById("txt_KiHieu").value;
    var name = document.getElementById("txt_TenTuyen").value;
    var stations = document.getElementById("txt_ChiTiet").value;
    updateBusRoute(idBusRoute, name, stations, number);
}

//khi ban chon một tuyến xe buýt trong combox thì sẽ vẽ tuyến xe đó ra và duy chuyển bản đồ tới vị trí đầu tiên của tuyến
function _getBusRouteToDraw(combo) {
    map.closeInfoWindow(); //nếu các cửa sổ WindownInfo đang mở thì close    
    var idBusRoute = combo.options[combo.selectedIndex].value; // ID của tuyến
    getBusRouteToDraw(idBusRoute);  // gọi hàm tương ứng với SERVICES

}

//tìm đương di giữa hai trạm
function _findPathBetweenTwoStation()
{

    var tramBatdau = document.getElementById("txtBatDau").value;
    var tramKetThuc = document.getElementById("txtKetThuc").value;
    var diBo = document.getElementById("txtDiBo").value;
    var chuyenTuyen = document.getElementById("txtChuyenTuyen").value;
    findPathBetweenTwoStation(tramBatdau, tramKetThuc, diBo, chuyenTuyen);
}                                                                     
function test() {
    var position1 = new VLatLng(10.7925225, 106.702544);
    var position2 = new VLatLng(10.8037991, 106.713616);
    drawLine(position1,position2)
}